package com.wwg.market.ui.category.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.TextBox;
import com.mobileares.midp.mui.client.form.UFishFlowForm;
import com.mobileares.midp.widgets.client.button.CButton;
import com.mobileares.midp.widgets.client.pop.PopPanel;
import com.mobileares.midp.widgets.client.textbox2.*;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.wwfish.gwt.model.client.IFishModel;
import com.wwfish.gwt.model.client.IModelCallback;
import com.wwfish.gwt.model.client.ISaveAndUpdateModelProvider;
import com.wwfish.gwt.model.client.impl.*;
import com.wwg.market.ui.category.client.MenuImpl;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.dashboard.client.utils.CacheCode;
import com.wwg.market.ui.service.dto.AttributeDto;
import com.wwg.market.ui.service.interfaces.ProductAttributeService;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-6
 * Time: 6:22:03
 * To change this template use File | Settings | File Templates.
 */
public class CategoryPropertyForm {
    private UFishFlowForm<AttributeDto> form;
    private PopPanel pop;
    private ListModel<AttributeDto> listModel;
    private PropertyItemWidget properties;
//    private CategoryDto category;

    private static String _LIST_TYPE = "0";

    public CategoryPropertyForm() {
        pop = new PopPanel(true);
        init();
        initFormModel();
    }

    public void setListModel(ListModel<AttributeDto> model) {
        this.listModel = model;
    }

    private void init() {
        form = new UFishFlowForm<AttributeDto>(1);
        TextBoxLabel text = new TextBoxLabel("属性名称:");
        final ListBoxLabel type = new ListBoxLabel("属性类型:");
        CheckBoxLabel required = new CheckBoxLabel("必填:");
        TextAreaLabel description = new TextAreaLabel("描述:");
        properties = new PropertyItemWidget();
        properties.addStyleName("property-code-wrapper");
        properties.setVisible(false);
        properties.setWidth("210px");

        form.addElement(text, "name", new ILabelValidator[]{LabelValidatorFactory.require()});
        form.addElement(type, "type", CacheCode.getCodesById(MenuImpl.categoryPropertyType), new ILabelValidator[]{LabelValidatorFactory.require()});
        form.addWidget(properties);
        form.addElement(required, "required");
        form.addElement(description, "description");
        pop.addBodyWidget(form);

        CButton confirm = new CButton("确定");
        confirm.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                form.getFishModel().saveAndUpdate(form.getFishWidget());
            }
        });
        CButton cancel = new CButton("取消");
        cancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                pop.hidden();
            }
        });
        pop.addFootButton(confirm);
        pop.addFootButton(cancel);

        ((TextBox) type.getLabelWidget().getWidget()).addValueChangeHandler(new ValueChangeHandler<String>() {
            public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
                if (_LIST_TYPE.equals(type.getValue())) {
                    properties.setVisible(true);
                } else
                    properties.setVisible(false);
            }
        });

    }

    public void show(AttributeDto dto) {
//        this.category = category;
        if (dto.getId() == null)
            pop.setHeadText("添加属性");
        else
            pop.setHeadText("编辑属性");

        form.getFishModel().load(dto);
        pop.show();
    }

    public void hidden() {
        pop.hidden();
    }

    private void initFormModel() {
        FormModel model = (FormModel) FishModelManager.createTempModel(FishModelManager.TYPE_FORM);
        model.setSaveAndUpdateProvider(new ISaveAndUpdateModelProvider<AttributeDto>() {
            public void saveAndUpdate(final AttributeDto value, final IModelCallback callback) {
                if (_LIST_TYPE.equals(value.getType())) {
                    value.setCodes(properties.getCodes());
                }
                Utils.invoke(ProductAttributeService.class.getName(), "saveAndUpdate", new Object[]{value}, new LoadingAsyncCallback() {
                    @Override
                    protected void success(Object o) {
                        hidden();
                        callback.setFishModel(o);
                        if (value.getId() == null)
                            listModel.addItem(0, (AttributeDto) o);
                        else
                            listModel.updateItem(value, (AttributeDto) o);

                    }
                });
            }
        });
        model.getListenerManager().addListener(Listener._LOAD, new ListenerHandler() {
            public void execute(IFishModel model) {
                AttributeDto dto = (AttributeDto) model.getModel();
                if (_LIST_TYPE.equals(dto.getType())) {
                    properties.setValue(dto.getCodes());
                } else {
                    properties.setValue(null);
                }
            }
        });
        form.setFishModel(model);
    }

}
